package com.example.his.api.mis.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.common.R;
import com.example.his.api.mis.controller.form.SearchGoodsByPageForm;
import com.example.his.api.mis.service.GoodsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author huixh
 */
@RestController
@RequestMapping("/mis/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @PostMapping("/searchByPage")
    @SaCheckPermission(value = {"ROOT","GOODS:SELECT"},mode = SaMode.OR)
    public R searchByPage(@RequestBody SearchGoodsByPageForm form) {
        Integer page = form.getPage();
        Integer length = form.getLength();
        int start = (page - 1) * length;
        Map<String, Object> map = BeanUtil.beanToMap(form);
        map.put("start",start);
        PageUtils pageUtils = goodsService.searchByPage(map);
        return R.resultOk().put("page", pageUtils);
    }

    @PostMapping("/uploadImage")
    @SaCheckPermission(value = {"ROOT","GOODS:INSERT","GOODS:UPDATE"},mode = SaMode.OR)
    public R uploadImage(@Param("file") MultipartFile file) {
        String url = goodsService.uploadImage(file);
        return R.resultOk().put("result", url);
    }
}
